<?php
require_once 'BaseController.php';
class Admin_NewsController extends BaseController
{
    public $_email = null;
    public $_role = null;
    protected $_arrParam = null;
    public function init()
    {
        $this->view->module = $this->_request->getModuleName();
        $this->view->controller = $this->_request->getControllerName();
        $this->view->action = $this->_request->getActionName();
        
        $this->_arrParam = $this->_request->getParams(); 
        unset($this->_arrParam['controller']);
        unset($this->_arrParam['action']);
        unset($this->_arrParam['model']);

        Zend_Loader::loadClass('AuthModel');
        Zend_Loader::loadClass('AdminModel');
        Zend_Loader::loadClass('RemoveCircumflex');
        Zend_Loader::loadClass('NewsModel');
        
        $auth = new AuthModel();
        $this->_email = $auth->getIdentityField('sunnet_email');
        $this->_role = $auth->getIdentityField('role');
    }
    public function indexAction(){
		$this->view->headTitle()->set("Quản trị Tin tức");
        if (isset($_GET['ajax'])) {
            $options = array('json' => 1);
            $ModelN = new NewsModel();
            $list_news = $ModelN->getListNewsAdmin('', $options);
            echo $list_news;
            exit();
        }
    }
    public function addAction(){
        
        if($this->_request->isPost()){
            $params = $this->_arrParam;
            
            $params['keyword'] = implode(',',$params['tags']);
            $this->view->object = $params;
            $date=new Zend_Date();
            $data=array(
                "title"=>$params['title'],
				"date"=>$date->toString("y-MM-dd"),
				"content"=>$params['content'],
                "keyword"=>$params['keyword'],
                "title_seo"=>$params['title_seo'],
                "description"=>$params['description'],
                "thumb" => $params['urlSelectImg'],
                "status" => $params['status'],
                "source" => $params['source'],
            );

            $ModelN = new NewsModel();   
            $ModelN->insert($data);
            $this->_redirect('/admin/news/index');
        }
    }
    public function editAction(){
        
        $ModelN = new NewsModel(); 
        $params = $this->_arrParam;
        if($this->_request->isPost()){
            
            $params['keyword'] = implode(',',$params['tags']);
            $this->view->object = $params;
            $date=new Zend_Date();
            $data=array(
                "title"=>$params['title'],
				"date"=>$date->toString("y-MM-dd"),
				"content"=>$params['content'],
                "keyword"=>$params['keyword'],
                "title_seo"=>$params['title_seo'],
                "description"=>$params['description'],
                "thumb" => $params['urlSelectImg'],
                "status" => $params['status'],
                "source" => $params['source']
            );  
            $ModelN->update($data,"id='".$params['id']."'");
            $this->_redirect('/admin/news/index');
        };
        
        $object = $ModelN->fetchRow("id='".$params['id']."'");
        $this->view->object = $object;
        //Zend_Debug::dump($object);die;
        
    }
    public function deleteAction(){
        $rows = $this->_request->getParam('checks');
        $ModelN = new NewsModel();

        $ModelN->multiDelete($rows);
        $this->_redirect('/admin/news/index');
        exit();
    }
    public function nominationAction()
    {
        $id = $_POST['data'];
        $nomination = $_POST['flag'];
        $ModelN = new NewsModel();
        $data = array('nomination' => $nomination);
        $where = array('id=?' => $id);
        $ModelN->update($data,$where);
        $number = count($ModelN->fetchAll("nomination = 1"));
        echo $number;
        die;
    }   
}